<script type="text/javascript">
/* 弹出确认框
* 前四位参数是传递给do_ajax函数
* tips 提示文字
*/
function do_confirm(form_id,btn_class,post_url,back_url,tips){
    // 默认提示删除文字
    if(tips == ''){
        tips = '{:lang('v_delete_sure')}';
    }
    // 弹出框
    var html =  modal_head;
        html += '<div class="modal-body py-4 text-center">';
        html += '    <span onclick="javascript:do_ajax(\''+form_id+'\',\''+btn_class+'\',\''+post_url+'\',\''+back_url+'\');" class="btn btn-danger mr-3 cursor"><i class="fas fa-check-circle mr-1"></i>'+tips+'</span>';
        html += '    <button class="btn btn-secondary" data-dismiss="modal"><i class="fas fa-times-circle mr-1"></i>{:lang('v_cancel')}</button>';
        html += '</div>';
    $('#modal_sm_content').removeClass('bg-opacity');
    $('#modal_sm_content').html(html);
    $("#modal_sm").modal("show");
}

/* ajax提交表单
* form_id 表单ID
* btn_class 按钮class
* post_url 提交URL
* back_url 返回URL
*/
function do_ajax(form_id,btn_class,post_url,back_url){
    if(form_id != '#form_second'){
        $('#modal').modal('hide');
    }
    $.ajax({
        type: 'POST',
        url: post_url,
        dataType: 'json',
        data: $(form_id).serialize(),
        beforeSend: function(){
            // 禁用按钮防止重复提交
            $(btn_class).attr({disabled: "disabled"});
            var html =  '<div class="modal-body py-3 text-white text-center text-md">';
                html += '    <img src="{:config('template.skin')}/img/loading-white.gif" width="18" height="18" class="mr-2">{:lang('v_doing')}';
                html += '</div>';
            $('#modal_sm_content').addClass('bg-opacity');
            $('#modal_sm_content').html(html);
            $("#modal_sm").modal("show");
        },
        success: function(data){
            //删除cookie，license
            $.cookie('bmkj_admin_user',null,{expires:-1,path:'/'});
            $(btn_class).removeAttr('disabled');
            $('#modal_sm').modal('hide');
            switch(data.code){
                // 操作成功
                case 200:
                    var html =  '<div class="modal-body py-3 text-white text-center text-md">';
                        html += '    <i class="fas fa-check-circle mr-2"></i>{:lang('c_success')}';
                        html += '</div>';
                    $('#modal_sm_content').html(html);
                    $("#modal_sm").modal('show');
                    switch(back_url){
                        case '':
                            window.location.reload();
                            break;
                        default:
                            window.location.href = back_url;
                    }
                    break;
                // 修改后入入口文件时用到
                case 2000:
                    edit_admin_file(data.data);
                    break;
                // 操作失败
                default:
                    var html =  modal_head;
                        html += '<div class="modal-body pt-3 pb-4 text-center text-md text-danger">';
                        html += '    <i class="fas fa-info-circle mr-2"></i>' + data.msg;
                        html += '</div>';
                    $('#modal_sm_content').removeClass('bg-opacity');
                    $('#modal_sm_content').html(html);
                    $("#modal_sm").modal('show');
            }
        },
    });
}

// 修改后入入口文件时用到
function edit_admin_file(data){
    var html =  '<div class="modal-body py-3 text-white text-center text-md">';
        html += '    <i class="fas fa-check-circle mr-2"></i>{:lang('c_success')}';
        html += '</div>';
    $('#modal_sm_content').html(html);
    $("#modal_sm").modal('show');
    window.location.href = '/' + data;
}
</script>